package smart.src;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Properties;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import org.openswing.swing.domains.java.Domain;
import org.openswing.swing.internationalization.java.EnglishOnlyResourceFactory;
import org.openswing.swing.mdi.client.ClientFacade;
import org.openswing.swing.mdi.client.Clock;
import org.openswing.swing.mdi.client.MDIController;
import org.openswing.swing.mdi.client.MDIFrame;
import org.openswing.swing.mdi.java.ApplicationFunction;
import org.openswing.swing.tree.java.OpenSwingTreeNode;
import org.openswing.swing.util.client.ClientSettings;

import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;


public class SmartP implements MDIController {

	OfficeClientFacade clientFac = null;
	private ConnectionSource conn = null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
		      public void run() {
		        new SmartP();
		      }
		    });
	}

	/**
	 * Create the application.
	 */
	public SmartP() {
		initialize();
		createConnection();
		clientFac = new OfficeClientFacade(conn);
		
	    Hashtable domains = new Hashtable();
	    Domain sexDomain = new Domain("SEX");
	    sexDomain.addDomainPair("M","male");
	    sexDomain.addDomainPair("F","female");
	    domains.put(
	      sexDomain.getDomainId(),
	      sexDomain
	    );
	    Properties props = new Properties();
	    ClientSettings clientSettings = new ClientSettings(
	            new EnglishOnlyResourceFactory("$",props,true),
	            domains
	        );
		MDIFrame mdi = new MDIFrame(this);
	}

	private void createConnection() {
		 String databaseUrl = "jdbc:mysql://localhost/smartP1";
	       
	        try {
	        	conn = new JdbcConnectionSource(databaseUrl);
	            ((JdbcConnectionSource) conn).setUsername("root");
	            ((JdbcConnectionSource) conn).setPassword("821355");    
	            
	        }catch (SQLException e) {
	            e.printStackTrace();
	        }
	    	
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
	}

	@Override
	public void afterMDIcreation(MDIFrame frame) {
		MDIFrame.addStatusComponent(new Clock());
	    //frame.addSeparatorToMenuBar("getEmployees2","getDepts");
		
	}

	@Override
	public String getMDIFrameTitle() {
		return "SmartPay";
	}

	@Override
	public int getExtendedState() {
		return JFrame.MAXIMIZED_BOTH;
	}

	@Override
	public String getAboutText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAboutImage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void stopApplication() {
		System.exit(0);	
	}

	@Override
	public boolean viewChangeLanguageInMenuBar() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList getLanguages() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean viewLoginInMenuBar() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public JDialog viewLoginDialog(JFrame parentFrame) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean viewFunctionsInTreePanel() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean viewFunctionsInMenuBar() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public ClientFacade getClientFacade() {
		// TODO Auto-generated method stub
		return clientFac;
	}

	@Override
	public DefaultTreeModel getApplicationFunctions() {
	    DefaultMutableTreeNode root = new OpenSwingTreeNode();
	    DefaultTreeModel model = new DefaultTreeModel(root);
	    ApplicationFunction n1 = new ApplicationFunction("Administration",null);
	    n1.setShortCut(new Character('A'));
	    ApplicationFunction n11 = new ApplicationFunction("Cards in grid+detail","getCards","men.gif","getCards");
	    n11.setShortCut(new Character('M'));
	    ApplicationFunction n12 = new ApplicationFunction("Status","getCardStatus","men.gif","getCardStatus");
	    n12.setShortCut(new Character('S'));
	    ApplicationFunction n13 = new ApplicationFunction("State","getCardState","men.gif","getCardState");
	    n13.setShortCut(new Character('T'));
	    ApplicationFunction n14 = new ApplicationFunction("Mode","getCardMode","men.gif","getCardMode");
	    n14.setShortCut(new Character('M'));
	    
	    ApplicationFunction n15 = new ApplicationFunction("New Client","setNewClient","men.gif","setNewClient");


	    n1.add(n11);
	    n1.add(n12);
	    n1.add(n13);
	    n1.add(n14);
	    n1.add(n15);

	    root.add(n1);

	    return model;
	}

	@Override
	public boolean viewOpenedWindowIcons() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean viewFileMenu() {
		// TODO Auto-generated method stub
		return false;
	}

}
